import UmiRequest from '@du/umi-request'
import { notification } from 'ant-design-vue'
const { requestInit } = UmiRequest
import router from '@/router'
import cache from '@/cache'

export default function initServer() {
  requestInit({
    // prefix: '',
    headers: {
      appVersion: '4.69.0',
      'x-auth-token': cache.get('x-auth-token'),
    },
    du: {
      secretKey: '',
      beforeRequestCB(options) {
        // 请求前
        // console.log('请求前',options)
      },
      afterResponseCB(options) {
        // 请求返回后
      },
      beforeRequestConfirmCB(options) {
        // 处理二次确认框
      },
      successCallback(data, options) {
        // 成功
      },
      errorCallback: (err, options, response) => {
        // console.log(err, options, response)
        const { status, data } = response
        // 登录失效
        if (status === 401) {
          router.push('/login')
          return false
        }
        const data_msg = data ? data.msg || data.message : ''

        notification.error({
          message: '错误',
          description:
            err || options.msg || response.message || data_msg || '接口出错',
        })
      },
    },
  })
}
